package com.example.demo.mapper;

import com.example.demo.domain.UserAccount;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

// UserAccountMapper.java
@Mapper
public interface UserAccountMapper {

    @Select("SELECT * FROM user_account WHERE user_id = #{userId}")
    UserAccount selectByUserId(Long userId);

    @Update("UPDATE user_account SET balance = #{balance} WHERE id = #{id}")
    int updateBalance(UserAccount account);

    // 强制读最新（加共享锁）
    @Select("SELECT * FROM user_account WHERE user_id = #{userId} LOCK IN SHARE MODE")
    UserAccount selectByUserIdWithShareLock(Long userId);

    // 强制读最新（加排他锁）
    @Select("SELECT * FROM user_account WHERE user_id = #{userId} FOR UPDATE")
    UserAccount selectByUserIdForUpdate(Long userId);
}